import express from "express"
import IoRedis from "ioredis"
import fs from "fs"
const lottery = fs.readFileSync("./lottery.lua", "utf-8")

const ioredis = new IoRedis({
    host: "localhost",
    port: 6379,
})
const app = express()

// 后端解决跨域问题
app.use('*', (req, res, next) => {
    // 允许所有来源
    res.header('Access-Control-Allow-Origin', '*')
    // 放行
    next()
})

const key = "lottery"
const time = 30 // 时间
const count = 5 // 次数

app.get("/lottery", async (req, res) => {
    ioredis.eval(lottery, 1, key, time, count, (err, result) => {
        if (err) {
            console.log(err)
            res.send("Error")
        }
        if (result === 0) {
            res.send("请稍后再试")
        }
        if (result === 1) {
            res.send("抽奖成功")
        }
    })

})

app.listen(3000, () => {
    console.log("Server is running on port 3000")
})